
class SoundManager {
    private static _me: SoundManager;
    public static get me(): SoundManager {
        if (SoundManager._me == null)
            SoundManager._me = new SoundManager();
        return SoundManager._me;
    }
    private _click: egret.Sound;//点击声音
    private _word: egret.Sound;//点击字块的声音
    private _right: egret.Sound;//如果胜利
    private _wrong: egret.Sound;//如果错误
    private _bgm: egret.Sound;//背景音乐
    private _bet: egret.Sound;//背景音乐
    private _begin: egret.Sound;//背景音乐
    private _timeover: egret.Sound;//背景音乐
    private _win: egret.Sound;//背景音乐
    private _fapai: egret.Sound;
    private _mopai: egret.Sound;

    private _boom: egret.Sound;
    public PlayTriggerSound(triggerType: number, _sex: number) {
        var url: string;

        if (_sex == GameConfig.conf.nan) {
            if (this.language == "0")
                url = "resource/assets/sound/man/putong/";
            else
                url = "resource/assets/sound/man/meizhou/";
        }
        this.PlaySoundByUrl(url);
    }


    private isBgm: boolean = false;
    private soundArr: string[] = [];
    private PlaySoundByUrl(url: string) {
        //开始加载
        if (this.IsSound == true) {
            if (this.soundCache[url] != null) {
                this.soundCache[url].play(0, 1);
            } else {
                var sound = new egret.Sound();
                sound.type = egret.Sound.EFFECT;

                //添加加载完成侦听
                sound.addEventListener(egret.Event.COMPLETE, this.onSoundLoadComplete, this);
                sound.load(url);

            }
        }
    }
    private onMusicLoadComplete(event: egret.Event): void {
        //获取加载到的 Sound 对象
        var sound: egret.Sound = <egret.Sound>event.target;
        if (this._music_channel != null)
            this._music_channel.stop();
        //播放音乐
        this._music_channel = sound.play(0, -1);
        this.soundCache[sound["url"]] = sound;
        this.completeMusic = sound;

        console.log("开始播放背景音乐" + this._music_channel.volume);
        this._music_channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);

    }
    private onSoundComplete(event: egret.Event): void {
        this.completeSound = null;
        this.comBool = true;
    }
    private onSoundLoadComplete(event: egret.Event): void {
        //获取加载到的 Sound 对象
        //播放音乐
        if (this.IsSound == true) {
            var sound: egret.Sound = <egret.Sound>event.target;
            var channel: egret.SoundChannel = sound.play(0, 1);
            channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);

            this.soundCache[sound["url"]] = sound;
            if (this.comBool == false)
                this.completeSound = sound;
        }
    }
    private comBool: boolean = false;
    private completeMusic: egret.Sound;
    private completeSound: egret.Sound;
    //特意做一个点击触发播放的方法
    public playComplete() {
        if (this.completeSound != null) {
            this.completeSound.play(0, 1);
            this.completeSound = null;
            this.comBool = true;
        }
    }

    private _bgm_channel: egret.SoundChannel;//保存用来静音用
    private _sound_channel: egret.SoundChannel;//保存用来静音用
    //设置音效音量
    public setSoundVolume(vo: number) {
        vo = vo / 100;
        this.soundVolume = vo.toString();
    }
    //设置背景音乐音量
    public setBgmVolume(vo: number) {
        vo = vo / 100;
        this._bgm_channel.volume = vo;
        this.musicVolume = vo.toString();
    }
    public set soundVolume(value) {
        egret.localStorage.setItem("soundVolume", value.toString());

    }
    public get soundVolume(): string {
        var b = egret.localStorage.getItem("soundVolume");
        if (b == null)
            return "1";
        return b;

    }
    //音乐是否播放，保存设置
    public set musicVolume(value) {
        egret.localStorage.setItem("musicVolume", value.toString());

    }
    public get musicVolume(): string {
        var b = egret.localStorage.getItem("musicVolume");
        if (b == null)
            return "1";
        return b;

    }
    public constructor() {

    }

    private _music_channel: egret.SoundChannel;//保存用来静音用
    private soundCache: Object = {};

    public curBackBg: string = "";
    public PlayMusicByUrl(_type: number = 1) {
        this.StopBGM();
        var url: string = "resource/assets/sound/background.mp3";
        if (_type == 2) {
            url = "resource/assets/sound/background2.mp3";
        }


        this.curBackBg = url;
        //开始加载
        if (this.soundCache[url] != null) {
            this._music_channel = this.soundCache[url].play(0, -1);
        } else {
            var sound = new egret.Sound();
            sound.type = egret.Sound.MUSIC;
            //添加加载完成侦听
            sound.addEventListener(egret.Event.COMPLETE, this.onMusicLoadComplete, this);
            sound.load(url);
        }
    }


    public StopBGM() {
        if (this._music_channel != null)
            this._music_channel.stop()
    }
    public PlayGameStart() {
        this.PlaySoundByUrl("resource/assets/sound/start.mp3");
    }
    public playjinbi() {
        var url: string = "resource/assets/sound/sound_jinbi.mp3";
        this.PlaySoundByUrl(url);
    }
    public playDianPao() {
        var url: string = "resource/assets/sound/effect/dianpao.mp3";
        this.PlaySoundByUrl(url);
    }

    public playTimeOver() {
        var url: string = "resource/assets/sound/Special_Remind.mp3";
        this.PlaySoundByUrl(url);
    }
    public playDownpai() {
        var url: string = "resource/assets/sound/shuffle.mp3";
        this.PlaySoundByUrl(url);
    }
    public playOffLine() {
        var url: string = "resource/assets/sound/leaveseat.mp3";
        this.PlaySoundByUrl(url);
    }
    public PlayBoom() {
        this.PlaySoundByUrl("resource/assets/sound/Special_Bomb.mp3");
    }
    public PlayChupai() {
        this.PlaySoundByUrl("resource/assets/sound/tileout.mp3");
    }
    public PlayMopai() {
        this.PlaySoundByUrl("resource/assets/sound/takecard.mp3");
    }
    public PlayClick() {
        this.PlaySoundByUrl("resource/assets/sound/effect/ui_button1.mp3");
    }
    public PlayCreateClick() {
        this.PlaySoundByUrl("resource/assets/sound/effect/ui_button1.mp3");
    }
    public PlayJoinClick() {
        this.PlaySoundByUrl("resource/assets/sound/effect/ui_button2.mp3");
    }
    public PlayShareClick() {
        this.PlaySoundByUrl("resource/assets/sound/effect/ui_fenxiang.ogg");
    }
    public PlayGangEffect() {
        this.PlaySoundByUrl("resource/assets/sound/effect/gang.mp3");
    }
    public PlayHuEffect() {
        this.PlaySoundByUrl("resource/assets/sound/effect/huzi.mp3");
    }
    public PlaySelect() {
        this.PlaySoundByUrl("resource/assets/sound/SEND_CARD.mp3");
    }
    public PlayRight() {
        this.PlaySoundByUrl("resource/assets/sound/sound_win.ogg");
    }
    public PlayWrong() {
        this.PlaySoundByUrl("resource/assets/sound/sound_fail.ogg");
    }
    public PlaySaizi() {
        this.PlaySoundByUrl("resource/assets/sound/saizi.mp3");
    }
    //点赞，你真牛逼
    public PlayDianzan(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_pijiu.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_pijiu.mp3");

    }
    //砸鸡蛋
    public PlayJidan(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_jidan.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_jidan.mp3");

    }
    //亲一个
    public PlayKiss(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_kiss.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_kiss.mp3");

    }
    //鲜花
    public PlayFlower(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_xianhua.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_xianhua.mp3");

    }
    //炸弹
    public PlayZhadan(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_zhadan.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_zhadan.mp3");

    }
    //拖鞋
    public PlayTuoxie(_sex: number) {
        if (_sex == GameConfig.conf.nan)
            this.PlaySoundByUrl("resource/assets/sound/M_tuoxie.mp3");
        else
            this.PlaySoundByUrl("resource/assets/sound/F_tuoxie.mp3");

    }
    public playReady() {
        var url: string = "resource/assets/sound/ready.mp3";
        this.PlaySoundByUrl(url);
    }
    //0 普通话  1 方言
    public set language(value: string) {
        egret.localStorage.setItem("language", value);
    }
    public get language(): string {
        var b: string = egret.localStorage.getItem("language");
        if (b == null || b == "") {
            return "0";
        }
        return b;
    }
    //0 双击  1 单击
    public set ClickType(value: string) {
        egret.localStorage.setItem("ClickType", value);
    }
    public get ClickType(): string {
        var b: string = egret.localStorage.getItem("ClickType");
        if (b == null || b == "") {
            return "1";
        }
        return b;
    }
    //音乐是否播放，保存设置
    public set IsMusic(value) {
        if (!value) {
            egret.localStorage.setItem("ismusic", "0");
            this.StopBGM();
        } else {
            egret.localStorage.setItem("ismusic", "1");
            if (1 == 1)
                this.PlayMusicByUrl(2);
            else
                this.PlayMusicByUrl(1);

        }
    }
    public get IsMusic(): boolean {
        var b = egret.localStorage.getItem("ismusic");
        if (b == null || b == "") {
            return true;
        }
        else {
            return b == "1";
        }
    }
    //声效是否播放，保存设置
    public set IsSound(value) {
        if (value) {
            egret.localStorage.setItem("isSound", "1");
        } else {
            egret.localStorage.setItem("isSound", "0");
        }
    }
    public get IsSound(): boolean {
        var b = egret.localStorage.getItem("isSound");
        if (b == null || b == "") {
            return true;
        }
        else {
            return b == "1";
        }
    }

    public PlayChatSound(ind: any, sex: number) {
        var url: string;
        if (sex == GameConfig.conf.nan) {
                url = "resource/assets/sound/man/meizhou/chat/chat_" + ind + ".mp3";
        }
        else {
                url = "resource/assets/sound/women/meizhou/chat/chat_" + ind + ".mp3";
        }

        this.PlaySoundByUrl(url);
    }

}
